package com.wenwei.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/10.
 */
public class MapUtils {

    public static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    public static void goLocation(Context context,String type ,double jin,double wei,String title){
        if(isAvilible(context,"com.baidu.BaiduMap")&&type.equals("baiDu")){
            Uri mUri = Uri.parse("geo:"+jin+","+wei+"?q="+title);
            Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
            context.startActivity(mIntent);
        }else if (isAvilible(context,"com.autonavi.minimap")&&type.equals("gaoDe")){
            Uri mUri = Uri.parse("geo:"+jin+","+wei+"?q="+title);
            Intent intent = new Intent("android.intent.action.VIEW",mUri);
            context.startActivity(intent);
        }else {
            Toast.makeText(context,"您还没有安装该地图",Toast.LENGTH_SHORT).show();
        }
    }

}
